import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import javax.swing.JSlider;
public class GraphPrima {
  private int i,j,v,to,inf,xr=40,yr=20,b=0;
  private int MAX_VERTS;
  private boolean used[];
  private int min_e[]; 
  private int sel_e[]; 
  private int xep[];
  //======================================================================
  public void Init(ReadFileClass file,mxGraph graph,Object parent) throws InterruptedException{
    MAX_VERTS=file.GetMaxVertex();
    inf = 1000000000;
    used = new boolean[MAX_VERTS];
    min_e = new int[MAX_VERTS];
    sel_e = new int[MAX_VERTS];
    xep = new int[MAX_VERTS];
    for(i=0;i<MAX_VERTS;i++){
        min_e[i]=inf;sel_e[i]=-1;xep[i] = -1;
        float theta = (float) (360/MAX_VERTS  * Math.PI * i / 180.0f);        
        graph.insertVertex(parent,null,i+1, 350+15*MAX_VERTS * Math.cos(theta),250+15*MAX_VERTS * Math.sin(theta), 24, 24,"shape=ellipse;");
        graph.setCellStyles(mxConstants.STYLE_FONTSIZE, "18",new Object[]{graph.getModel().getChildAt(parent,i)});
        graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "white",new Object[]{graph.getModel().getChildAt(parent,i)});
    }
    for(i=0;i<MAX_VERTS;i++){
         for(j=0;j<MAX_VERTS;j++){
             if(file.matr[i][j]!=inf){ 
                  graph.insertEdge(parent,null,file.matr[i][j],graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j),"startArrow=none;endArrow=none;");
                  graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "black",graph.getEdgesBetween(graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j)));     
                  graph.setCellStyles(mxConstants.STYLE_FONTSIZE, "15",graph.getEdgesBetween(graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j)));
                  graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "red",graph.getEdgesBetween(graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j)));
             }
         }
    }
  }
  //======================================================================
  public void SortPrima(ReadFileClass file,mxGraph graph,Object parent,mxGraph twograph,Object parent2,int mychoicevert,JSlider slider) throws InterruptedException{
    for(i=0;i<MAX_VERTS;i++){
        min_e[i]=inf;sel_e[i]=-1;used[i]=false;
    }
   b=0;xr=40;yr=20;
   min_e[mychoicevert-1] = 0;
   for (i=0; i<MAX_VERTS; ++i) {
    v = -1;
    for (j=0; j<MAX_VERTS; ++j)
      if (!used[j] && (v == -1 || min_e[j] < min_e[v]))
	v = j;
	if (min_e[v] == inf) {
                 ErrorWin f = new ErrorWin();
                 f.createGUI("No MST.");
                 return;
	}
	used[v] = true; 
	if (sel_e[v] != -1){
            graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "blue",graph.getEdgesBetween(graph.getModel().getChildAt(parent,v),graph.getModel().getChildAt(parent,sel_e[v]-1)));
            Thread.sleep(slider.getValue());
            twograph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "pink",twograph.getEdgesBetween(twograph.getModel().getChildAt(parent2,xep[v]-2),twograph.getModel().getChildAt(parent2,xep[v]-1)));
             twograph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "pink", new Object[]{twograph.getModel().getChildAt(parent2,xep[v]-2)});
             twograph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "pink", new Object[]{twograph.getModel().getChildAt(parent2,xep[v]-1)});
            Thread.sleep(slider.getValue());
        }
        graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "blue", new Object[]{graph.getModel().getChildAt(parent,v)});
        Thread.sleep(slider.getValue()); 
	for (to=0; to<MAX_VERTS; ++to){
                if(file.matr[v][to]!=inf && !used[to]){
                twograph.insertVertex(parent2,null,v+1,xr,yr, 24, 24,"shape=ellipse;");
                twograph.insertVertex(parent2,null,to+1,xr+100,yr, 24, 24,"shape=ellipse;");
                twograph.insertEdge(parent2,null,file.matr[v][to],twograph.getModel().getChildAt(parent2,b),twograph.getModel().getChildAt(parent2,b+1),"startArrow=none;endArrow=none;");
                twograph.setCellStyles(mxConstants.STYLE_FONTSIZE, "18",twograph.getChildCells(parent2));
                twograph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "black",twograph.getChildCells(parent2));
                yr+=30;b+=3;
                }
            if (file.matr[v][to] < min_e[to]) {
		min_e[to] = file.matr[v][to];
                xep[to] = b-1;
		sel_e[to] = v+1;
            }
         }
        Thread.sleep(slider.getValue());
     }
  }
  public int retmaxverts(){
       return MAX_VERTS;
   }
}